package com.nanyang.tms.service.shipment.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillAddRequest;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillFuzzyQueryRequest;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillQueryRequest;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillUpdateStateRequest;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillDetailResponse;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillFuzzyPageResponse;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillPageResponse;
import com.nanyang.tms.api.service.shipment.ShipmentBillRpcService;
import com.nanyang.tms.dao.shipment.ShipmentBillDao;
import com.nanyang.tms.dao.shipment.ShipmentRelationDao;
import com.nanyang.tms.domain.shipment.ShipmentBillDO;
import com.nanyang.tms.domain.shipment.ShipmentBillQuery;
import com.nanyang.tms.domain.shipment.ShipmentBillVO;
import com.nanyang.tms.domain.shipment.ShipmentRelationVO;
import com.nanyang.tms.manager.shipment.ShipmentBillManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class ShipmentBillRpcServiceImpl implements ShipmentBillRpcService {
    @Autowired
    private ShipmentBillManager shipmentBillManager;
    @Autowired
    private ShipmentBillDao shipmentBillDao;
    @Autowired
    private ShipmentRelationDao shipmentRelationDao;
    @Override
    public ApiResult<PageBean<ShipmentBillPageResponse>> listShipmentBill(ShipmentBillQueryRequest param) {
        ApiResult<PageBean<ShipmentBillPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            ShipmentBillQuery query = new ShipmentBillQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<ShipmentBillVO> shipmentList = shipmentBillManager.listShipmentBill(query);
            PageBean<ShipmentBillPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(shipmentList.getTotalItem());
            BeanUtils.copyListProperties(shipmentList.getData(), resultList.getData(), ShipmentBillPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<PageBean<ShipmentBillFuzzyPageResponse>> listFuzzyShipmentBill(ShipmentBillFuzzyQueryRequest param) {
        ApiResult<PageBean<ShipmentBillFuzzyPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            ShipmentBillQuery query = new ShipmentBillQuery();
            BeanUtils.copyProperties(param, query);
            query.setState(0);
            PageBean<ShipmentBillVO> shipmentList = shipmentBillManager.listShipmentBill(query);
            PageBean<ShipmentBillFuzzyPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(shipmentList.getTotalItem());
            BeanUtils.copyListProperties(shipmentList.getData(), resultList.getData(), ShipmentBillFuzzyPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<List<ShipmentBillDetailResponse>> selectByCode(String shipmentCode) {
        ApiResult<List<ShipmentBillDetailResponse>> result = new ApiResult<>();
        try {
            List<ShipmentBillDetailResponse> shipmentBillDetailResponseList = new ArrayList<>();
            List<ShipmentRelationVO> shipmentRelationVOS = shipmentRelationDao.selectByCode(shipmentCode);
            if(CollectionUtil.isNotEmpty(shipmentRelationVOS)){
                BeanUtils.copyListProperties(shipmentRelationVOS, shipmentBillDetailResponseList, ShipmentBillDetailResponse.class);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( shipmentBillDetailResponseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveShipmentBill(ShipmentBillAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ShipmentBillDO shipmentDo = new  ShipmentBillDO();
            BeanUtils.copyProperties(param,shipmentDo);
            shipmentDo.setGmtCreatePin(param.getPin());
            shipmentDo.setShipmentTime(DateUtil.parseDateTime(param.getShipmentTime()));
            shipmentBillManager.saveShipmentBill(shipmentDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.WAYBILL,template="shipmentBillState")
    @Override
    public ApiResult<Boolean> updateState(ShipmentBillUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ShipmentBillDO shipmentDo = new  ShipmentBillDO();
            BeanUtils.copyProperties(param,shipmentDo);
            shipmentDo.setGmtModifiedPin(param.getPin());
            shipmentBillDao.updateState(shipmentDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> cancelState(ShipmentBillUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            ShipmentBillDO shipmentDo = new  ShipmentBillDO();
            BeanUtils.copyProperties(param,shipmentDo);
            shipmentDo.setGmtModifiedPin(param.getPin());
            shipmentBillManager.cancelState(shipmentDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
